package com.tnovoselec.android.mojevijesti.async;

import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import android.app.ProgressDialog;
import android.content.Context;
import android.os.AsyncTask;
import android.util.Log;

import com.nth.android.Util;
import com.tnovoselec.android.mojevijesti.R;
import com.tnovoselec.android.mojevijesti.dto.ChannelDto;
import com.tnovoselec.android.mojevijesti.dto.ChannelsResultHolder;
import com.tnovoselec.android.mojevijesti.parser.ChannelParser;

public class GetChannelsAsync extends AsyncTask<Void, Long, List<ChannelDto>> {

	public interface ChannelsDownloadedListener {
		public void onChannelsDownloaded();
	}

	private ChannelsDownloadedListener listener;

	public static String TAG = GetChannelsAsync.class.getSimpleName();
	
	public static long CACHE_VALIDITY = 15 * 60 * 1000;

	private String xmlUrl;
	private String pathToSave;
	private boolean fromCache;
	private Context context;
	
	protected ProgressDialog dialog = null;

	public GetChannelsAsync(String xmlUrl, String pathToSave, boolean fromCache, Context context) {
		this.xmlUrl = xmlUrl;
		this.pathToSave = pathToSave;
		this.fromCache = fromCache;
		this.context = context;
	}
	
	@Override
	protected void onPreExecute() {
		super.onPreExecute();
		dialog = ProgressDialog.show(context, "", context.getResources().getText(R.string.please_wait_channels), true);
	}

	@Override
	protected List<ChannelDto> doInBackground(Void... params) {
		HttpURLConnection connection = null;
		InputStream is = null;
		List<ChannelDto> items;
		try {

			if (Util.fileExistsHex(xmlUrl, context) && (System.currentTimeMillis() - Util.getFileHex(xmlUrl, context).lastModified()) < CACHE_VALIDITY) {
				// is = Util.readFile(xmlUrl, context);
			} else if (!fromCache) {
				// TODO ubaciti u Util framework
				URL imageUrl = new URL(xmlUrl);
				connection = (HttpURLConnection) imageUrl.openConnection();
				connection.setConnectTimeout(Util.HTTP_CONNECT_TIMEOUT);
				connection.setReadTimeout(Util.HTTP_READ_TIMEOUT);
				connection.setDoInput(true);
				connection.connect();

				is = connection.getInputStream();
				Util.fileCreate(xmlUrl, is, context);
			}
			if (Util.fileExistsHex(xmlUrl, context)) {
				is = Util.readFile(xmlUrl, context);
			} else {
				return null;
			}

			ChannelParser parser = new ChannelParser();
			SAXParserFactory factory = SAXParserFactory.newInstance();
			SAXParser sp = factory.newSAXParser();
			sp.parse(is, parser);
			items = new ArrayList<ChannelDto>();
			items = parser.getChannels();
			ChannelsResultHolder.init(items);
		} catch (Exception ex) {
			Log.e(TAG, ex.getMessage(), ex);
			return null;
		}

		return items;
	}

	@Override
	protected void onPostExecute(List<ChannelDto> result) {
		super.onPostExecute(result);
		if (listener != null) {
			listener.onChannelsDownloaded();
		}
		dialog.dismiss();
	}

	public void setListener(ChannelsDownloadedListener listener) {
		this.listener = listener;
	}

}
